import { Log } from "../debug/Log";
type int = number;
//class FilterText
    
    export  class FilterText /* flash.events.EventDispatcher */
    {
        constructor()
        {
            this._splitReg = new RegExp("(\\n|\\r)+", "mg");
            this._filterRegAry = [];
            this._loader = new Object /* flash.net.URLLoader */();
            this._wordMap = new Map<any, any> /* flash.utils.Dictionary */();
            return;
        }

        public load(arg1: string): void
        {
            var file: string;
            var request: Object /* flash.net.URLRequest */;

            var loc1;
            file = arg1;
            request = new Object /* flash.net.URLRequest */(file);
            try 
            {
                this._loader.load(request);
                this.addListener();
            }
            catch (error)
            {
                Log.system("Unable to load requested document.");
            }
            return;
        }

        /* internal  */addListener(): void
        {
            this._loader.addEventListener(flash.events.Event.COMPLETE, this.completeHandler);
            this._loader.addEventListener(flash.events.IOErrorEvent.IO_ERROR, this.ioErrorHandler);
            return;
        }

        /* internal  */removelistener(): void
        {
            this._loader.removeEventListener(flash.events.Event.COMPLETE, this.completeHandler);
            this._loader.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, this.ioErrorHandler);
            return;
        }

        /* internal  */completeHandler(arg1: Object /* flash.events.Event */): void
        {
            this.log("加载完成");
            this.removelistener();
            var loc1=flash.net.URLLoader(arg1.target);
            this.createRegExpStr(String(loc1.data));
            dispatchEvent(arg1);
            return;
        }

        /* internal  */createRegExpStr(arg1: string): void
        {
            var loc1=arg1.split(this._splitReg);
            var loc2=loc1.length;
            var loc3=0;
            while (loc3 < loc2) 
            {
                this.addWord(loc1[loc3]);
                ++loc3;
            }
            return;
        }

        public ioErrorHandler(arg1: Object /* flash.events.IOErrorEvent */): void
        {
            Log.system(arg1);
            dispatchEvent(arg1);
            return;
        }

        public getFilterStr(arg1: string): string
        {
            var loc2=null;
            var loc3=undefined;
            if (null == arg1 || arg1 == "") 
            {
                return "";
            }
            var loc1=arg1.length;
            var loc4=0;
            while (loc4 < loc1) 
            {
                loc2 = arg1.charAt(loc4);
                if (loc2 != "*") 
                {
                    if ((loc3 = this._wordMap[loc2]) is String) 
                    {
                        var loc5;
                        this._wordMap[loc2] = loc5 = new RegExp("(" + loc3 + ")", "img");
                        loc3 = loc5;
                    }
                    arg1 = arg1.replace(loc3, this.regHandler);
                }
                ++loc4;
            }
            return arg1;
        }

        public isHaveFilterTxt(arg1: string): Boolean
        {
            var loc1=this.getFilterStr(arg1);
            return !(loc1 == arg1);
        }

        /* internal  */regHandler(): string
        {
            var loc1=arguments[1].toString();
            return loc1.replace(new RegExp(".{1}", "g"), "*");
        }

        /* internal  */log(arg1: Object): void
        {
            Log.system(arg1);
            return;
        }

        public setFilterStr(arg1: string, arg2: RegExp=null): void
        {
            if (arg2 != null) 
            {
                this._splitReg = arg2;
            }
            this.createRegExpStr(arg1);
            return;
        }

        public clearWords(): void
        {
            this._wordMap = new Map<any, any> /* flash.utils.Dictionary */();
            return;
        }

        public addWord(arg1: string): void
        {
            var loc1=null;
            var loc2=null;
            arg1 = arg1.replace("\n", "");
            if (!(arg1 == null) && arg1.length > 0) 
            {
                loc2 = arg1.charAt(0);
                loc1 = this._wordMap[loc2] as string;
                if (loc1) 
                {
                    this._wordMap[loc2] = this._wordMap[loc2] + ("|" + arg1);
                }
                else 
                {
                    this._wordMap[loc2] = arg1;
                }
            }
            return;
        }

        public static get instance(): FilterText
        {
            if (FilterText._instance == null) 
            {
                FilterText._instance = new FilterText();
            }
            return FilterText._instance;
        }

        private /* const */MAX_LENGTH: int=1000;

        private /* var */_loader: Object /* flash.net.URLLoader */;

        private /* var */_filterStr: string;

        private /* var */_filterRegExp: RegExp;

        private /* var */_splitReg: RegExp;

        private /* var */_wordMap: Map<any, any> /* flash.utils.Dictionary */;

        private /* var */_filterRegAry: Array<any>;

        private static /* var */_instance: FilterText;
    }
